package com.dunshan.prometh.controller;

import com.dunshan.prometh.popj.Result;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Random;

/**
 * @author dunshan
 * @program: dunshan-pef
 * @description: page
 * @date 2021-09-25 21:33:59
 */
@Log4j2
@Controller
@RestController
public class IndexPromenthController {


    @Resource
    private PrometheusCustomMonitor monitor;


    @GetMapping("/")
    public Result IndexPage() {
        HashMap<Object, Object> map = new HashMap<>();
        map.put("data", "全链路压测业务监控");
        return Result.buildSuccess(map);
    }


    /**
     * 模拟订单数据
     *
     * @param num
     * @return
     */
    @RequestMapping("/order/{num}")
    public Result orderTotalNumber(@PathVariable int num) {
        // 统计下单次数
        if (num == 1) {
            monitor.getOrderCount().increment();
        } else {
            //失败数据
            monitor.getFailureCount().increment();
        }
        Random random = new Random();
        int amount = random.nextInt(108);
        // 统计金额
        monitor.getAmountSum().record(amount);
        HashMap<String, Object> map = new HashMap<>();
        map.put("total", amount);
        map.put("time", System.currentTimeMillis());
        return Result.buildSuccess(map);
    }

}
